#include "tWidget.h"

#include "notify_adaptor.h"
#include "notify_interface.h"

#include <fstream>
//#include <iostream>

using namespace std;

QString readPath(){
    QString p, ret;
    string stream;
    p=QDir::homePath()+"/.opendesk/config/look";
    qWarning(p.toStdString().c_str());
    ifstream fin(p.toStdString().c_str());
    while (fin >> stream) {
        if (stream[0]=='#') {
            qWarning(stream.c_str());
        }
        if (stream=="ICONS") {
            fin >> stream;
            p = QString::fromStdString(stream);
            return p;
        }
    }
    p="/usr/pixmaps/opendesk/icons/";
    return p;
}

QString readPix(){
    QString p, ret;
    string stream;
    p=QDir::homePath()+"/.opendesk/config/look";
    qWarning(p.toStdString().c_str());
    ifstream fin(p.toStdString().c_str());
    while (fin >> stream) {
        if (stream[0]=='#') {
            qWarning(stream.c_str());
        }
        if (stream=="PIXMAPS") {
            fin >> stream;
            p = QString::fromStdString(stream);
            return p;
        }
    }
    p="/usr/pixmaps/opendesk/icons/";
    return p;
}


QBadge::QBadge(QString App, QString Text, QString Alert, QImage Icon, bool Alarm, bool Badge, QWidget * parent ): app(App), text(Text), alert(Alert), icon(Icon), alarm(Alarm), badge(Badge), QAbstractButton(parent){
    stdLaunch = false; //beacouse no apps made..
    if (stdLaunch) {
        connect(this, SIGNAL(clicked()), this, SLOT(launch()));
    }
}


QBadge::~QBadge(){}

void QBadge::setAlarm(bool Alarm){
    alarm = Alarm;
    update();
}

void QBadge::setBadge(bool Badge){
    badge = Badge;
    update();
}

void QBadge::setAlert(QString Alert) {
    alert= Alert;
    update();
}

void QBadge::setLaunch(bool Launch){
    stdLaunch= Launch;
}

void QBadge::paintEvent(QPaintEvent * event) {
    QPainter p(this);
    p.setRenderHint(QPainter::TextAntialiasing);
    p.setRenderHint(QPainter::Antialiasing);

    //p.fillRect(event->rect(), Qt::black);

    qreal size = 30;		// Badge Size

    QRadialGradient redGradient(0.0, 0.0, 17.0, size - 3, size - 3);
    redGradient.setColorAt(0.0, QColor(0xe0, 0x84, 0x9b));
    redGradient.setColorAt(0.5, QColor(0xe9, 0x34, 0x43));
    redGradient.setColorAt(1.0, QColor(0xdc, 0x0c, 0x00));
    QRadialGradient blueGradient(0.0, 0.0, 17.0, size - 3, size - 3);
    blueGradient.setColorAt(0.0, QColor(0x6f, 0x9d, 0xe8));
    blueGradient.setColorAt(0.5, QColor(0x4a, 0x72, 0xbf));
    blueGradient.setColorAt(1.0, QColor(0x27, 0x47, 0x94));

    p.save();
    p.drawImage(5, 5, QImage(icon));
    if (badge) {
        if (alarm) {
            drawBadge(p, 1, 1, 30, alert, QBrush(redGradient));
        }
        else drawBadge(p, 1, 1, 30, alert, QBrush(blueGradient));
    }
    drawText(p);
    p.restore();

    p.end();
}

void QBadge::fillEllipse (QPainter& p,int x, int y,int size,const QBrush& brush)
{
        QPainterPath path;
        path.addEllipse(x, y, size, size);
        p.fillPath(path, brush);
}

void QBadge::drawBadge (QPainter& p,int x, int y,int size,const QString& text,const QBrush& brush)
{
        p.setFont(QFont(font().family(), 11, QFont::Bold));
        while ((size - p.fontMetrics().width(text)) < 6) {
                int pointSize = p.font().pointSize() - 1;
                int weight = (pointSize < 6) ? QFont::Normal : QFont::Bold;
                p.setFont(QFont(p.font().family(), p.font().pointSize() - 1, weight));
        }

        QColor shadowColor(0x00, 0x00, 0x00, size);
        fillEllipse(p, x + 1, y, size, shadowColor);
        fillEllipse(p, x - 1, y, size, shadowColor);
        fillEllipse(p, x, y + 1, size, shadowColor);
        fillEllipse(p, x, y - 1, size, shadowColor);

        p.setPen(QPen(Qt::white, 2));
        fillEllipse(p, x, y, size - 3, brush);
        p.drawEllipse(x, y, size - 3, size - 3);

        p.setPen(QPen(Qt::white, 1));
        p.save();
        p.drawText(x, y, size - 3, size - 3, Qt::AlignCenter, alert);
        p.restore();
}

void QBadge::drawText(QPainter & p) {
    int size,x,y, h;
    size = p.fontMetrics().width(text);
    x=0;
    y=60;
    p.setPen(Qt::black);
    h =  p.fontMetrics().height();
    if (size < 60) {
        p.drawText(x + 3 , y +6 , 60 , h, Qt::AlignCenter, text);
    }
    else {
        int diff;
        diff = size -60;
        p.drawText(x+(diff) +3, y + 6, 60 + diff , h, Qt::AlignCenter, text);
    }
}

void QBadge::launch(){
    if (stdLaunch) {
       system(app.toStdString().c_str());
    }
}

QPhoneIcon::QPhoneIcon(QWidget * parent) : QBadge("phone","","",QImage(readPath()+"apps/phone.png"),false, false, parent){

    incomingCalls=0;

    new NotifyAdaptor(this);
    QDBusConnection::sessionBus().registerObject("/", this);
    com::opendesk::notify *iface;
    iface = new com::opendesk::notify(QString(), QString(),QDBusConnection::sessionBus(),this);

    connect(iface,SIGNAL(call(QString)), this, SLOT(newCall()));
}

QPhoneIcon::~QPhoneIcon(){}

void QPhoneIcon::newCall(){
    incomingCalls++;
    QString b = QString::number(incomingCalls);
    setAlert(b);
    setBadge(true);
}

QMessageIcon::QMessageIcon(QWidget * parent) : QBadge("message","","",QImage(readPath()+"apps/message.png"), false, false, parent){
    unreadMessages= 0;

    new NotifyAdaptor(this);
    QDBusConnection::sessionBus().registerObject("/", this);
    com::opendesk::notify *iface;
    iface = new com::opendesk::notify(QString(), QString(),QDBusConnection::sessionBus(),this);

    connect(iface,SIGNAL(message(QString,QString)), this, SLOT(newMessage()));
}

QMessageIcon::~QMessageIcon(){}

void QMessageIcon::newMessage(){
    unreadMessages++;
    QString b = QString::number(unreadMessages);
    setAlert(b);
    setBadge(true);
}

QBar::QBar(QWidget * parent) : QWidget(parent) {
    QImage icon("resurces/pixmaps/Icons/contacts.png");
    QPhoneIcon * phoneIcon = new QPhoneIcon(this);
    phoneIcon->setGeometry(13,11-2,68+5,68+5);
    badgeList.push_back(phoneIcon);
    QMessageIcon * messageIcon =new QMessageIcon(this);
    messageIcon->setGeometry(13+(25+68),11-2,68+5,68+5);
    badgeList.push_back(messageIcon);
    QBadge * contactsIcon = new QBadge("contacts","","",QImage(readPath()+"apps/contacts.png"),false, false, this);
    contactsIcon->setGeometry(13+(25+68)*2, 11-2, 68+5, 68+5);
    badgeList.push_back(contactsIcon);
    QBadge * settingsIcon = new QBadge("settings","","",QImage(readPath()+"apps/settings.png"),false, false,this);
    settingsIcon->setGeometry(13+(25+68)*3, 11-2, 68+5, 68+5);
    badgeList.push_back(settingsIcon);
    appsIcon = new QBadge("apps", "","", QImage(readPath()+"apps/apps.png"), false, false, this);
    appsIcon->setGeometry(13+(25+68)*4, 11-2, 68+5, 68+5);
    badgeList.push_back(appsIcon);
}

QBar::~QBar(){
    for (int i=0; i<badgeList.size(); i++) {
        delete badgeList.at(i);
    }
    badgeList.clear();
}

void QBar::addBadge(QLauncher launcher) {
    if (badgeList.size()   < 5) {
        QBadge * badge = new QBadge(launcher.apps,launcher.text, launcher.alert, launcher.Icon, launcher.alarm, launcher.badge, this);
        badge->setGeometry(13+(13+68)*badgeList.size(),11,68,68);
        badgeList.push_back(badge);
    }
}

void QBar::paintEvent(QPaintEvent * event){
   QPainter p(this);
   p.setRenderHint(QPainter::TextAntialiasing);
   p.setRenderHint(QPainter::Antialiasing);

   p.save();
   p.drawImage(0,0, QImage(readPix()+"resurces/bar.png"));
   p.restore();

   p.end();
}
